package com.qianji.controller;

import com.qianji.pojo.ActiveDto;
import com.qianji.pojo.ParticipationRecordDto;
import com.qianji.service.RecordService;
import com.qianji.vo.RecordCheck;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.beans.Transient;
import java.util.HashMap;
import java.util.Map;

/**
 * 判断参与活动总数是否达到规定可领奖次数的倍数
 * @Author 钟丽诗
 * @Date 2021.4.1
 */

@Slf4j
@RestController
@RequestMapping("/record/")
public class RecordController {

    @Autowired
    private RecordService recordService;

    @RequestMapping("check")
    public synchronized Map<String,Object> check(@RequestBody RecordCheck activeDto){

        Map<String , Object> resultMap = new HashMap<>();

        log.info("入参：{}",activeDto);

        ParticipationRecordDto recordDto = new ParticipationRecordDto(activeDto.getData().getId());

        Integer status = recordService.addRecord(recordDto);
        Long count = recordService.activityRecordCount(activeDto.getData().getId());
        if(status != 1){
            resultMap.put("message","插入数据失败");
            resultMap.put("code",400);
            resultMap.put("success",false);
            return resultMap;
        }

        if(count != 0 && count % activeDto.getData().getWinningNumber() == 0){
            resultMap.put("message","是"+activeDto.getData().getWinningNumber()+"的倍数");
            resultMap.put("code",200);
            resultMap.put("success",true);

            return resultMap;
        }
        resultMap.put("message","不是"+activeDto.getData().getWinningNumber()+"的倍数");
        resultMap.put("code",200);
        resultMap.put("success",false);
        return resultMap;
    }


}
